<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Alban Technologies. Written by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/xmlfeed.php";

if ($loadsubpage == 1)
{
  $pid = isset ($_GET['pid']) ? $_GET['pid'] : 0;
  
  /* Get site name */
  $result = mysql_query ("SELECT sitename, domainname FROM ".db_maketablename ($table_settings)." LIMIT 1");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      $sitename = htmlentities ($row['sitename'], ENT_COMPAT, "UTF-8", false);
      $domainname = htmlentities ($row['domainname'], ENT_COMPAT, "UTF-8", false);
    }
  mysql_free_result ($result);
?>
<h1><?=$sitename?></h1>

<div id="sitetree">
  <h3><?=$domainname?></h3>
  <table cellpadding="0" cellspacing="0" style="width: 100%">
<?php
/* Print the site tree */
$result = mysql_query ("SELECT id, path FROM ".db_maketablename ($table_pages)." WHERE path LIKE '[root]' ORDER BY path");
if (mysql_num_rows ($result) > 0)
  {
    $row = mysql_fetch_array ($result);
    print "<tr><td colspan=\"2\"><a href=\"./?p=site&amp;pid={$row['id']}\"><strong>/</strong> <span class=\"smalltext\">&rarr; Root element</span></a></td></tr>\n";
  }
mysql_free_result ($result);

$result = mysql_query ("SELECT id, path FROM ".db_maketablename ($table_pages)." ORDER BY path");
if (mysql_num_rows ($result) > 0)
  {
    while ($row = mysql_fetch_array ($result))
      {
        if ($row['path'] != "[root]")
          {
            $path = "/".htmlentities ($row['path'], ENT_COMPAT, "UTF-8", false);
            print "<tr><td><a href=\"./?p=site&amp;pid={$row['id']}\"><strong>$path</strong></a></td>";
            print "<td class=\"alignright\"><a href=\"javascript:delpage('$path', {$row['id']});\" title=\"Delete '$path'\"><strong>&times;</strong></a></td></tr>\n";
          }
      }
  }
mysql_free_result ($result);
?>
  </table>
  
  <div id="addnew1">
    <p>
      <input type="button" value="+" onclick="document.getElementById('addnew2').style.display='block';" class="button" />
      Add new page
    </p>
  </div>
  
  <div id="addnew2">
    <form action="../includes/addpage.php" method="post">
      <table cellpadding="0" cellspacing="0">
        <tr>
          <td><strong>Path:</strong></td>
          <td><input type="text" name="path" maxlength="255" value="/Type_path_here" class="textfield" /></td>
        </tr>
        <tr>
          <td><strong>Template:</strong>&nbsp;</td>
          <td>
            <select name="template">
<?php
/* Print the list of templates */
$result = mysql_query ("SELECT id, name FROM ".db_maketablename ($table_templates)." ORDER BY name");
if (mysql_num_rows ($result) > 0)
  {
    while ($row = mysql_fetch_array ($result))
      print "<option value=\"{$row['id']}\">".htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false).".xsl</option>\n";
  }
mysql_free_result ($result);
?>
            </select>
          </td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="Add &raquo;" class="button" /></td>
        </tr>
      </table>
    </form>
  </div>
</div>

<div id="editor">
<?php
/* If no page is selected... */
if ($pid == 0)
{
?>
  <div id="info">
    <p>&larr; Click on an item in the site tree to edit it.</p>
  </div>
<?php
}
else
{
?>
    <div id="tabs">
      <ul>
        <li><a href="javascript:switchtabs ('tab1');">Page</a></li>
        <li><a href="javascript:switchtabs ('tab2');">Settings</a></li>
      </ul>
    </div>
    
    <form action="../includes/savepage.php" method="post">
      <div id="tab1">
<?php
  $tbl_pages = db_maketablename ($table_pages);
  $tbl_templates = db_maketablename ($table_templates);
  $result = mysql_query ("SELECT path, fields, template, xmlfeed FROM $tbl_pages LEFT JOIN $tbl_templates ON $tbl_templates.id=$tbl_pages.template WHERE $tbl_pages.id='$pid'");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      
      /* Handle path */
      $path = $row['path'] == "[root]" ? "/" : "/{$row['path']}";
      $disabledpath = $row['path'] == "[root]" ? " disabled=\"disabled\"" : "";
      $templateid = $row['template'];
     
      /* Parse fields and xmlfeed */
      print "<label>Title</label><br /><input type=\"text\" name=\"title\" value=\"".htmlentities (xmlfeed_title ($row['xmlfeed']), ENT_COMPAT, "UTF-8", false)."\" class=\"textfield\" />\n";
      
      /* Prepare the XML parser and parse the data */
      $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$row['fields'];     
      $xmlres = xml_parser_create ("utf-8");
      xml_parse_into_struct ($xmlres, $xmldata, $values);
      xml_parser_free ($xmlres);
      
      /* Walk through the tags */
      $i = 0;
      while ($values[$i])
        {
          if ($values[$i]['level'] == 2 && strcasecmp ($values[$i]['tag'], "field") == 0 && isset ($values[$i]['attributes']['NAME']) && isset ($values[$i]['attributes']['LABEL']) && isset ($values[$i]['attributes']['TYPE']))
            {
              print "<label>{$values[$i]['attributes']['LABEL']}</label><br />";
              if ($values[$i]['attributes']['TYPE'] == "text")
                print "<input type=\"text\" name=\"{$values[$i]['attributes']['NAME']}\" value=\"".xmlfeed_value ($row['xmlfeed'], $values[$i]['attributes']['NAME'])."\" class=\"textfield\" />\n";
              else if ($values[$i]['attributes']['TYPE'] == "html")
                {
                  print "<textarea id=\"htmleditor\" name=\"{$values[$i]['attributes']['NAME']}\">".xmlfeed_value ($row['xmlfeed'], $values[$i]['attributes']['NAME'])."</textarea>\n";
                }
            }
          $i++;
        }
    }
  mysql_free_result ($result);
?>
      </div>
      
      <div id="tab2">
        <label>Path</label><br />
        <input type="text" name="path" value="<?=htmlentities ($path, ENT_COMPAT, "UTF-8", false)?>"<?=$disabledpath?> class="textfield" /><br />
        <label>Active template</label><br />
        <select name="template">
<?php
/* Print the list of templates */
$result = mysql_query ("SELECT id, name FROM ".db_maketablename ($table_templates)." ORDER BY name");
if (mysql_num_rows ($result) > 0)
  {
    while ($row = mysql_fetch_array ($result))
      {
        $selected = $row['id'] == $templateid ? " selected=\"selected\"" : "";
        print "<option value=\"{$row['id']}\"$selected>".htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false).".xsl</option>\n";
      }
  }
mysql_free_result ($result);
?>
        </select>
      </div>
      
      <p class="alignright">&nbsp;<br /><input type="hidden" name="pid" value="<?=$_GET['pid']?>" /><input type="submit" value="Save" /></p>
    </form>
<?php
}
?>
</div>

<div class="clear"></div>
<?php
}
?>
